package com.android.cl.coludtf.service.asynctask;

import android.content.Context;
import android.content.Intent;

import com.android.cl.coludtf.activity.Login;
import com.android.cl.coludtf.activity.Main;
import com.android.cl.coludtf.model.BaseBO;
import com.android.cl.coludtf.model.Result;
import com.android.cl.coludtf.model.Tenants;
import com.android.cl.coludtf.model.User;
import com.android.cl.coludtf.model.User_Tenant;
import com.android.cl.coludtf.network.HttpHelper;
import com.android.cl.coludtf.utils.sharedPreferences.DatabaseHelper;
import com.android.cl.coludtf.utils.toast.ToastUtil;
import com.google.gson.Gson;

import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LoginTask extends BaseAsyncTask {
    Login context;
    BaseBO bo;
    Gson gson;
    HttpHelper httpHelper;
    Result result = new Result();

    public LoginTask(Context context) {
        super(context);
        this.gson = new Gson();
        this.httpHelper = new HttpHelper();
    }

    public LoginTask(Login context) {
        super(context);
        this.context = context;
        this.gson = new Gson();
        this.httpHelper = new HttpHelper();
    }

    public LoginTask(Login context, String title, String content) {
        super(context, title, content);
        this.context = context;
        this.gson = new Gson();
        this.httpHelper = new HttpHelper();
    }

    @Override
    Result excuteTask(BaseBO... params) throws IOException {
        bo = params[0];
        bo.postBody = gson.toJson(bo.maps);
        return httpHelper.post(bo.requestUrl, bo.token, bo.postBody);
    }

    /**
     * 异步方法成功后，执行
     */
    @Override
    protected void onPostExecute(Result result) {
        try {
            postTask(result);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 获取初始信息后更新本地保存信息，然后跳转到Main
     *
     * @param result
     * @throws JSONException
     */
    void postTask(Result result) throws JSONException {

        if (result != null) {
            if (result.getCode() != null) {
                if (result.getCode().equals("200")) {
                    if (result.getData() != null) {
                        String s_user = gson.toJson(result.getData());
                        User user = gson.fromJson(s_user, User.class);
                        bo.token = user.getActiveUser().getToken();
                        //处理租户列表
                        List<Tenants> Tenants = new ArrayList<>();
                        Tenants tenants;
                        if (user.getTenantList() != null) {
                            for (User_Tenant tenant : user.getTenantList()) {
                                tenants = new Tenants();
                                tenants.setId(tenant.getId());
                                tenants.setTenantName(tenant.getTenantName());
                                tenants.setTenantCode(tenant.getTenantCode());
                                if (user.getActiveUser().getTenantId().equals(tenant.getId())) {
                                    tenants.setChecked(true);
                                } else {
                                    tenants.setChecked(false);
                                }
                                Tenants.add(tenants);
                            }
                            bo.tenants = gson.toJson(Tenants);
                        }
                        DatabaseHelper.updateBo(bo);

                        Intent mainIntent = new Intent(context, Main.class);
                        mainIntent.putExtra("index", "0");
                        ToastUtil.showTextShort("登录成功！");

                        context.startActivity(mainIntent);
                        context.finish();

                    } else {
                        ToastUtil.showTextShort("用户名或密码错误，重新输入");
                        context.InitView(1);
                    }
                } else {
                    ToastUtil.showTextShort(result.getMessage());
                }
            } else {
                ToastUtil.showTextShort("登录错误！");
            }
        } else {
            ToastUtil.showTextShort("登录错误！");
        }

    }

    @Override
    void ok() {
        super.ok();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}
